package www.nfin.ya.utils.impl;

import org.junit.Test;
import www.nfin.ya.service.impl.ExecuteSqlOracle;
import www.nfin.ya.utils.SqlDynamicExtractUtil;
import www.nfin.ya.utils.SuperOracleUtils;
import www.nfin.ya.utils.TableInfoExtractUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class OracleTableInfoExtractUtils implements TableInfoExtractUtils {

    public void reload(Connection connection){
        reloadTableInfos(connection);
        reloadColumnInfos(connection);
    }
    public void reloadTableInfos(Connection connection){
        ResultSet resultSet = null;
        try {
            resultSet = SuperOracleUtils.executeQuery("SELECT * FROM user_tab_comments");
            List<LinkedHashMap> tInfos = SqlDynamicExtractUtil.extractKV(resultSet);
            for (LinkedHashMap info : tInfos) {
                Map<TableEnum,Object> value = new HashMap<>();
                value.put(TableEnum.COMMENTS,info.get("COMMENTS"));
                tableInfos.put(((String) info.get("TABLE_NAME")).toLowerCase(),value);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
    public void reloadColumnInfos(Connection connection){
        ResultSet resultSet = null;
        try {

            resultSet = SuperOracleUtils.executeQuery("SELECT * FROM user_col_comments");
            List<LinkedHashMap> tInfos = SqlDynamicExtractUtil.extractKV(resultSet);
            for (LinkedHashMap info : tInfos) {
                Map<ColumnEnum,Object> value = new HashMap<>();
                value.put(ColumnEnum.COMMENTS,info.get("COMMENTS"));
                columnInfos.put(( info.get("TABLE_NAME")+":"+(String)info.get("COLUMN_NAME")).toLowerCase(),value);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
    @Test
    public void testReload() throws SQLException, ClassNotFoundException {
        ExecuteSqlOracle executeSqlOracle = new ExecuteSqlOracle();
        TableInfoExtractUtils tableInfoExtractUtils = new OracleTableInfoExtractUtils();
        tableInfoExtractUtils.reload(SuperOracleUtils.initDefaultConnection());
        System.out.println("s");
    }

}
